package com.coms514.scheduleme;

import java.io.IOException;

import com.google.api.client.googleapis.extensions.android.gms.auth.GooglePlayServicesAvailabilityIOException;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;

import android.os.AsyncTask;
import android.util.Log;
import android.view.View;

/** 
* @author Qilin Li
* 
* */

abstract class EventsAsyncTask  extends AsyncTask<Void, Void, Boolean> {

	/**
	 * @param args
	 */
	  final ShowEventsActivity  activity;
	  final com.google.api.services.calendar.Calendar client;
	  EventModel model;
	  private final View progressBar;

	  EventsAsyncTask(ShowEventsActivity activity) {
	    this.activity = activity;
	    client = activity.client;
	    model = activity.model;
	    progressBar = activity.findViewById(R.id.events_refresh_progress);
	  }

	  @Override
	  protected void onPreExecute() {
	    super.onPreExecute();
	    activity.numAsyncTasks++;
	    progressBar.setVisibility(View.VISIBLE);
	  }

	  @Override
	  protected final Boolean doInBackground(Void... ignored) {
		    try {
		        doInBackground();
		        return true;
		      } catch (IOException e) {
		        Utils.logAndShow(activity, CalendarSampleActivity.TAG, e);
		      }
		      return false;
	  }

	  @Override
	  protected final void onPostExecute(Boolean success) {
	    super.onPostExecute(success);
	    if (0 == --activity.numAsyncTasks) {
	      progressBar.setVisibility(View.GONE);
	    }
	    if (success) {
			activity.refreshView();
	    }
	  }

	  abstract protected void doInBackground() throws IOException;
}
